$(document).ready(function(){
	/*$(".easyui-validatebox").each(function(){
		if($(this).attr("data-options") != "" && $(this).attr("data-options").indexOf("required") != -1 || $(this).attr("required") == "required")
		{
			$(this).after('<label for="' + this.id + '" style="line-height:25px; padding-left:5px;">*</label>');
		}
	});*/
	$("form").not("#logout-form").submit(function() {
		return false;
	});
	$(".data_con #datagrid").css("height",$("body").height() - ($(".sel_div").height() + 40) + "px");
	$.ajaxSetup({
		beforeSend: function() {
			this.url += (this.url.indexOf("?") == -1? "?":"&") + "rand=" + Math.random();
		},
		dataFilter: function(data,type){
			//将ajax请求获取回来的数据中的<>字符转换成html编码，用于表格数据显示，避免html标签注入
			data = JSON.parse(JSON.stringify(data).replace(/</g, "&lt;").replace(/>/g, "&gt;"));
			return data;
		},
		complete: function(xhr,status) {
		    if(xhr.getResponseHeader && xhr.getResponseHeader("sessionStatus") == 'timeout' || 
		    		(xhr.responseJSON && xhr.responseJSON.errorCode == "USER_NO_LOGIN")) {
		      //$(".messager-window").close();
		      $(".messager-window .panel-tool-close").click();
		      $.messager.alert('提示','<p>会话已失效, 请重新登录！</p>', 'error', function(){
		    	  window.parent.location.href = '/UserLogin.html';
		      });
		    }
		}
	});
	$("#datagrid").datagrid({
		onLoadSuccess: function(data) {
			//将表格里的html编码还原成原始字符，用于表单显示
			var rowDatas = $.data($("#datagrid")[0],"datagrid").data.rows;			
			$.data($("#datagrid")[0],"datagrid").data.rows = JSON.parse(JSON.stringify(rowDatas).replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
		},
		onLoadError: function() {
			$.messager.alert("错误","<p>服务异常，查询数据失败，请重新登录或稍后再试！</p>","error");	
		}
	});
	formatTime("#txtBeginTime","00:00:00");
	formatTime("#txtEndTime","23:59:59");
	if(getParamobjFromUrl(window.location.href).model === "view")
	{
		var arrUrl = window.location.href.split("/");
		var strPage = arrUrl[arrUrl.length - 1];
		var searchVal = window.location.search;
		strPage = strPage.replace(searchVal,"").split(".")[0];
		if(strPage === "FieldSystemInterfaceManage")
		{
			$(".right_con input").removeClass("easyui-validatebox validatebox-text validatebox-invalid").attr({"disable":"disable","readonly":true});
			$(".right_con textarea").attr({"disable":"disable","readonly":true});
			$(".right_con #instance_save").remove();
			
		}else
		{
			$("#toolbox").remove();
			$(".datagrid-header-check").text("").parent("td").remove();
			$(".datagrid-cell-check").parent("td").remove();
		}
	}
});
//combobox选择验证
function comboValidate(targetId,validateTxt)
{
	targetId = targetId.replace("#","");
	var html = '<input id="' + targetId + 'Hidden" name="' + targetId + 'Hidden"  class="easyui-validatebox text" data-options="required:true" missingMessage="' + validateTxt + '不能为空!" style="position:absolute; z-index:-1; left:0; top:0" data-hasVal="false"/>';
	$("#" + targetId).parent(".input_con").append(html);
	$("#" + targetId + "Hidden").focus(function(e) {
		if($(this).attr("data-hasVal") !== "true")
		{
			setTimeout(function(){$("#" + targetId + "Hidden").blur();$("#" + targetId + "Hidden").val('') },1500);
		}
	});
}
//删除全选框
setTimeout(function(){$(".datagrid-header-check").text("");},200);
//格式化时间控件
function formatTime(target,time)
{
	$(target).datetimebox({ 
		required : false,
		editable:false,
		onShowPanel:function(){ 
			if($(this).datetimebox("getValue") === '')
			{
				$(this).datetimebox("spinner").timespinner("setValue",time); 
			}else
			{
				$(this).datetimebox("spinner").timespinner("setValue",$(this).datetimebox("spinner").timespinner("getValue"));
			}
		}
	});
}
//表单跟着浏览器缩放
setTimeout(function()
{
	if(!$("body").hasClass("easyui-layout"))
	{
		$("body").css("overflow","auto");
	}
	$("#datagrid").datagrid("resize");
},1000);
$(window, parent.document).resize(function(e) {
	$("#datagrid").datagrid("resize");
});

//清空开始时间的同时清空结束时间
$.fn.datetimebox.defaults.buttons[2].handler = function(_a6h) {
	$(_a6h).datetimebox('setValue', '').datetimebox('setText', '');
	$(_a6h).datetimebox("hidePanel");
	$(_a6h).parents("td").siblings("td").children("#txtBeginTime, #txtEndTime").datetimebox('setValue', '').datetimebox('setText', '');
	$(this).closest("div.combo-panel").panel("close");
}

var varify; //用于查询验证,验证开始时间是否小于结束时间
//搜索判断时间范围选择
function dataRetrieval(beginTimeTarget,endTimeTarget)
{
	var txtBeginTime = $(beginTimeTarget).datetimebox('getValue');
	if(!varify)
	{
		$(endTimeTarget).combo('setValue', '').combo('setText', '');
	}
	var txtEndTime = $(endTimeTarget).datetimebox('getValue');
	
	if((txtBeginTime === '' && txtEndTime === '') || varify || (txtBeginTime !== '' && txtEndTime === '') || (txtBeginTime === '' && txtEndTime !== ''))
	{
		$('#datagrid').datagrid('load');
	}else{
		$.messager.alert('警告', '<p>结束时间要大于开始时间</p>', 'warning');
	}
}



/*时间选择器距离缩近*/
setTimeout(function()
{
	$(".easyui-datetimebox").siblings(".textbox").children(".validatebox-text").css("letter-spacing","-0.1em");
},1000);

(function($){  
    $.fn.serializeJson=function(){  
        var serializeObj={};  
        $(this.serializeArray()).each(function(){  
            serializeObj[this.name]=this.value;  
        });  
        return serializeObj;  
    };  
})(jQuery);

function ShowMessageBox(title, msg, icon) {
	$(".panel-tool-close", $(".messager-body").parent()).click();
	$.messager.show({
		showSpeed:300,
		title:title,
		msg:msg,
		icon:icon,
		timeout:2000,
		height:'auto',
		style:{
			left:"",
			top:15+document.body.scrollTop+document.documentElement.scrollTop,
			right:15,
			bottom:""
		}
	});
}

function getParamobjFromUrl(url) {
	var urlParamStr = "";
	if(url)
	{
		urlParamStr = url.substring(url.indexOf("?") + 1);
	}
	else
	{
		urlParamStr = window.location.search.substr(1);
	}
	var urlParamArr = urlParamStr.split("&");
	var paramObj = {};
	for(var i = 0; i < urlParamArr.length; ++i)
	{
		var param = urlParamArr[i].split("=");
		if(param.length < 2)
		{
			continue;
		}
		paramObj[param[0]] = param[1];
	}
	return paramObj;
}

//数据检索区域重置方法
function onReset() {
	$(".sel_con input").not("input[readonly], .combobox-f").each(function(){
		$(this).val("");
	});
	$(".combobox-f").combobox('setValue','');
	$(".easyui-datetimebox").combo('setValue', '').combo('setText', '');
}

function getCookie(c_name)
{
	var cookieStr = document.cookie.replace(/\s/g, '');
	if (cookieStr.length>0)
	{ 
		var cookieStrArr = cookieStr.split(";");
		var cookieObj = {};
		for(var i = 0; i < cookieStrArr.length; ++i)
		{
			var keyValuePair = cookieStrArr[i].split("=");
			if(keyValuePair[0])
			{
				cookieObj[keyValuePair[0]] = keyValuePair[1];
			}
		}
		var cookieValue = cookieObj[c_name];
		if(cookieValue)
		{
			return unescape(cookieValue);
		}
		/*c_start=cookieStr.indexOf(c_name + "=");
		if (c_start!=-1 && cookieStr.charAt(c_start - 1) == '' ||)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=cookieStr.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}*/ 
	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null)
		document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
